package com.googlecode.horncomb.web.extremetable.custom;

import net.sf.json.JSONObject;

import org.extremecomponents.table.bean.Table;
import org.extremecomponents.table.core.PreferencesConstants;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.AbstractHtmlView;
import org.extremecomponents.util.HtmlBuilder;
import org.springframework.util.StringUtils;

public class SimpleLinkView extends AbstractHtmlView {
	public static final String STYLECLASS_SHOW_ALL_ROWS = "showAllRows";
	public static final String STYLECLASS_SHOW_BTM_TOOLBAR = "showBottomToolBar";

	public final static String TABLE_SHOW_ALL_ROWS = "table.showAllRows";
	public final static String TABLE_SHOW_BTM_TOOLBAR = "table.showBottomToolBar";

	private Boolean showAllRows = Boolean.TRUE;
	private Boolean showBottomToolBar = Boolean.FALSE;

	@Override
	protected void init(HtmlBuilder html, TableModel model) {
		super.init(html, model);

		// add 'showAllRows' and 'showBottomToolBar' attributes to table
		Table table = model.getTableHandler().getTable();
		String styleClass = table.getStyleClass();
		String strJsonObj = null;
		if (styleClass != null && styleClass.indexOf("{") != -1
				&& styleClass.indexOf("}") > 0) {
			strJsonObj = styleClass.substring(styleClass.indexOf("{"),
					styleClass.indexOf("}") + 1);
		}
		JSONObject jsonData = JSONObject.fromObject(strJsonObj);

		// get default 'showAllRows' from preferences properties
		String strShowAllRows = model.getPreferences().getPreference(
				TABLE_SHOW_ALL_ROWS);
		Boolean showAllRows = strShowAllRows == null ? this.showAllRows
				: Boolean.valueOf(strShowAllRows);
		// get 'showAllRows' from styleClass in page's tag
		showAllRows = jsonData.isNullObject() ? showAllRows : jsonData
				.optBoolean(STYLECLASS_SHOW_ALL_ROWS, showAllRows);

		// get default 'showBottomToolBar' from preferences properties
		String strShowBtmTBar = model.getPreferences().getPreference(
				TABLE_SHOW_BTM_TOOLBAR);
		Boolean showBtmTBar = strShowBtmTBar == null ? this.showBottomToolBar
				: Boolean.valueOf(strShowBtmTBar);
		// get 'showBottomToolBar' from styleClass in page's tag
		showBtmTBar = jsonData.isNullObject() ? showBtmTBar : jsonData
				.optBoolean(STYLECLASS_SHOW_BTM_TOOLBAR, showBtmTBar);

		table.addAttribute(STYLECLASS_SHOW_ALL_ROWS, showAllRows);
		table.addAttribute(STYLECLASS_SHOW_BTM_TOOLBAR, showBtmTBar);

		// if styleClass is empty afte deleting jsonObj, set default value
		if (StringUtils.trimAllWhitespace(
				StringUtils.delete(styleClass, strJsonObj)).length() == 0) {
			styleClass = model.getPreferences().getPreference(
					PreferencesConstants.TABLE_STYLE_CLASS);
			table.setStyleClass(styleClass);
		} else {
			styleClass = StringUtils.replace(styleClass, strJsonObj, " ");
			table.setStyleClass(styleClass);
		}
	}

	@Override
	protected void beforeBodyInternal(TableModel model) {
		toolbar(getHtmlBuilder(), getTableModel());

		getTableBuilder().tableStart();

		getTableBuilder().theadStart();

		getTableBuilder().titleRowSpanColumns();

		getTableBuilder().filterRow();

		getTableBuilder().headerRow();

		getTableBuilder().theadEnd();

		getTableBuilder().tbodyStart();
	}

	@Override
	protected void afterBodyInternal(TableModel model) {
		getCalcBuilder().defaultCalcLayout();

		getTableBuilder().tbodyEnd();

		getTableBuilder().tableEnd();

		if ((Boolean) model.getTableHandler().getTable().getAttribute(
				STYLECLASS_SHOW_BTM_TOOLBAR)
				&& model.getLimit().getTotalRows() > 0) {
			toolbar(getHtmlBuilder(), getTableModel(), true);
		}
	}

	protected void toolbar(HtmlBuilder html, TableModel model) {
		new LinkToolbar(html, model).layout();
	}

	protected void toolbar(HtmlBuilder html, TableModel model, boolean bottom) {
		new LinkToolbar(html, model, bottom).layout();
	}
}
